package com.zjw.zy.utils;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.RemoteViews;

import androidx.annotation.NonNull;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;

import com.zjw.zy.R;
import com.zjw.zy.bean.HourlyWeather;
import com.zjw.zy.bean.NearWeather;
import com.zjw.zy.bean.Tip;
import com.zjw.zy.bean.Weather1Day;
import com.zjw.zy.bean.WsMessage;
import com.zjw.zy.constant.NotificationArgsKey;
import com.zjw.zy.constant.SpecialDay;
import com.zjw.zy.MainActivity;
import com.zjw.zy.constant.WechatMessageSubInfo;
import com.zjw.zy.voice.VoiceService;

import java.util.Date;

/**
 * @author ：zhong.jw
 * @date ：Created in 2022/9/27 15:22
 */
public final class NotificationUtils {

    private static final String TAG = "NotificationUtils";

    public static final String BUNDLE_KEY = "NotificationUtils_BUNDLE_KEY";

    private final static String WEATHER_CHANNEL = "WEATHER_CHANNEL";
    private final static String DAY_CHANNEL = "DAY_CHANNEL";
    private final static String TIP_CHANNEL = "TIP_CHANNEL";
    private final static String WECHAT_CHANNEL = "WECHAT_CHANNEL";

    private static final int WEATHER_NOTIFY_ID = 0;
    private static final int DAY_NOTIFY_ID = 1;
    private static final int TIP_NOTIFY_ID = 2;
    private static final int WECHAT_NOTIFY_ID = 3;

    public static void initChannel(@NonNull Context context) {
        //创建渠道
        NotificationChannel channel = new NotificationChannel(WEATHER_CHANNEL, "天气", NotificationManager.IMPORTANCE_DEFAULT);
        NotificationChannel channel2 = new NotificationChannel(DAY_CHANNEL, "日子", NotificationManager.IMPORTANCE_DEFAULT);
        NotificationChannel channel3 = new NotificationChannel(TIP_CHANNEL, "事项", NotificationManager.IMPORTANCE_HIGH);
        NotificationChannel channel4 = new NotificationChannel(VoiceService.VOICE_CHANNEL_ID, "语音", NotificationManager.IMPORTANCE_HIGH);
        NotificationChannel channel5 = new NotificationChannel(WECHAT_CHANNEL, "消息", NotificationManager.IMPORTANCE_HIGH);
        channel.setDescription("天气通知");
        channel2.setDescription("日子通知");
        channel3.setDescription("事项通知");
        channel4.setDescription("语音通话");
        channel5.setDescription("消息通知");
        //设置震动
        channel.enableVibration(true);
        channel2.enableVibration(true);
        channel3.enableVibration(true);
        channel4.enableVibration(true);
        channel5.enableVibration(true);
        NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);
        notificationManager.createNotificationChannel(channel2);
        notificationManager.createNotificationChannel(channel3);
        notificationManager.createNotificationChannel(channel4);
        notificationManager.createNotificationChannel(channel5);
    }


    public static void sendTipNotification(@NonNull Context context, @NonNull Tip tip) {
        Log.d(TAG, "sendTipNotification() called with: context = [" + context + "], tip = [" + tip + "]");
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, TIP_CHANNEL);

        String contentText = tip.tip;
        builder.setContentTitle("洋洋~事项提醒~")
                //todo 此处差tip图标
                .setSmallIcon(R.drawable.icon_browser)
                .setContentText(contentText)
                //.setContentIntent(getActivity)
                .setAutoCancel(true)
                .setPriority(NotificationCompat.PRIORITY_HIGH);

        NotificationManagerCompat.from(context).notify(TIP_NOTIFY_ID, builder.build());
    }


    /**
     * 通知天气
     */
    public static void sendWeatherNotification(@NonNull Context context, @NonNull Weather1Day today) {
        Log.d(TAG, "sendWeatherNotification() called with: context = [" + context + "], today = [" + today + "]");

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, WEATHER_CHANNEL);

        Intent intent = new Intent(context, MainActivity.class);
        intent.putExtra(BUNDLE_KEY, getBundle(WEATHER_CHANNEL));
        PendingIntent getActivity = PendingIntent.getActivity(context, 1, intent,
                PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE, getBundle(WEATHER_CHANNEL));

        HourlyWeather hourlyWeather = today.hourlyFcsts.get(0);
        HourlyWeather rainHourlyWeather = null;
        boolean hasRain = false;
        for (HourlyWeather hourlyFcst : today.hourlyFcsts) {
            if (hourlyFcst.text.contains("雨")) {
                hasRain = true;
                rainHourlyWeather = hourlyWeather;
                break;
            }
        }
        String contentText = "现在~" + hourlyWeather.text + (hasRain ? "，今日" + rainHourlyWeather.getTime() + "会下雨哦~" : "，今日不会下雨");

        builder.setContentTitle("洋洋~早安~")
                .setSmallIcon(WeatherUtils.INSTANCE.getWeatherIconResId(hourlyWeather.text, true))
                .setContentText(contentText)
                .setContentIntent(getActivity)
                .setAutoCancel(true)
                .setPriority(NotificationCompat.PRIORITY_DEFAULT);

        NotificationManagerCompat.from(context).notify(WEATHER_NOTIFY_ID, builder.build());
    }

    /**
     * 通知特殊天气
     */
    public static void sendNearWeatherNotification(@NonNull Context context, @NonNull NearWeather nearWeather) {

        Log.d(TAG, "sendNearWeatherNotification() called with: context = [" + context + "], nearWeather = [" + nearWeather + "]");

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, WEATHER_CHANNEL);

        Intent intent = new Intent(context, MainActivity.class);
        intent.putExtra(BUNDLE_KEY, getBundle(WEATHER_CHANNEL));
        PendingIntent getActivity = PendingIntent.getActivity(context, 1, intent,
                PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE, getBundle(WEATHER_CHANNEL));

        builder.setContentTitle(nearWeather.getTitle())
                .setSmallIcon(WeatherUtils.INSTANCE.getWeatherIconResId(nearWeather.getText(), nearWeather.getHourlyWeather().isDay()))
                .setContentText(nearWeather.getText())
                .setContentIntent(getActivity)
                .setAutoCancel(true)
                .setPriority(NotificationCompat.PRIORITY_HIGH);

        NotificationManagerCompat.from(context).notify(WEATHER_NOTIFY_ID, builder.build());
    }


    /**
     * 通知节假日
     */
    public static void sendDayNotification(@NonNull Context context) {

        Log.d(TAG, "sendDayNotification() called with: context = [" + context + "]");

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, DAY_CHANNEL);

        Intent intent = new Intent(context, MainActivity.class);
        intent.putExtra(BUNDLE_KEY, getBundle(DAY_CHANNEL));
        PendingIntent getActivity = PendingIntent.getActivity(context, 1, intent,
                PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);


        TimeUtils.TimeBean timeBean = TimeUtils.currentTime();

        String special = timeBean.getSpecial();
        if (!SpecialDay.NULL.equals(special)) {
            builder.setContentTitle(timeBean.getLunar().toString())
                    .setSmallIcon(R.drawable.icon_browser)
                    .setContentText(special)
                    .setAutoCancel(true)
                    .setContentIntent(getActivity)
                    .setPriority(NotificationCompat.PRIORITY_DEFAULT);
        } else {
            builder.setContentTitle(timeBean.getStringOfDay())
                    .setSmallIcon(R.drawable.icon_browser)
                    .setContentText(timeBean.getDayOfWeekStr())
                    .setContentIntent(getActivity)
                    .setAutoCancel(true)
                    .setPriority(NotificationCompat.PRIORITY_DEFAULT);
        }
        NotificationManagerCompat.from(context).notify(DAY_NOTIFY_ID, builder.build());
    }

    /**
     * 通知节假日
     */
    public static void sendWechatNotification(@NonNull Context context, WsMessage message) {

        Log.d(TAG, "sendWechatNotification() called with: message = [" + message + "]");

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, WECHAT_CHANNEL);

        Intent intent = new Intent(context, MainActivity.class);
        intent.putExtra(BUNDLE_KEY, getBundle(WECHAT_CHANNEL));
        PendingIntent getActivity = PendingIntent.getActivity(context, 1, intent,
                PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);

        String text = "";
        String subInfo = message.getInfo().substring(WsMessage.WECHAT_MESSAGE_INFO.length());
        if (subInfo.equals(WechatMessageSubInfo.TEXT)) {
            text = message.getText();
        } else if (subInfo.equals(WechatMessageSubInfo.IMAGE)) {
            text = "[图片]";
        }

        builder.setContentTitle("来自亲爱的消息")
                .setSmallIcon(R.drawable.icon_browser)
                .setContentText(text)
                .setAutoCancel(true)
                .setContentIntent(getActivity)
                .setPriority(NotificationCompat.PRIORITY_DEFAULT);

        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.notify_wechat_layout);
        remoteViews.setImageViewResource(R.id.notify_wechat_face, R.drawable.love_girl_yy_2);
        remoteViews.setTextViewText(R.id.notify_wechat_context, text);
        remoteViews.setTextViewText(R.id.notify_wechat_time, TimeUtils.formatDate(new Date(message.getDate()), "yyyy/MM/dd HH:mm:ss"));
        builder.setContent(remoteViews);

        NotificationManagerCompat.from(context).notify(WECHAT_NOTIFY_ID, builder.build());
    }


    @NonNull
    private static Bundle getBundle(String channel) {
        Bundle bundle = new Bundle();
        switch (channel) {
            case WEATHER_CHANNEL:
                bundle.putInt(NotificationArgsKey.TARGET_FRAGMENT, R.id.nav_main_fragment);
                break;
            case DAY_CHANNEL:
                bundle.putInt(NotificationArgsKey.TARGET_FRAGMENT, R.id.nav_calendar_fragment);
                break;

            case WECHAT_CHANNEL:
                bundle.putInt(NotificationArgsKey.TARGET_FRAGMENT, R.id.nav_wechat_fragment);
                break;
        }

        return bundle;
    }
}
